package driver

import (
	"github.com/gin-gonic/gin"
	v1 "golang-sgg-daijia/api/v1"
	"golang-sgg-daijia/internal/handler"
	"golang-sgg-daijia/internal/service"
	"net/http"
)

type DriverHandler struct {
	*handler.Handler
	driverService service.DriverService
}

func NewDriverHandler(
	handler *handler.Handler,
	driverService service.DriverService,
) *DriverHandler {
	return &DriverHandler{
		Handler:       handler,
		driverService: driverService,
	}
}

// Login 司机端微信小程序登录接口
func (h *DriverHandler) Login(ctx *gin.Context) {
	code := ctx.Param("code")
	if code == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	token, err := h.driverService.Login(ctx, code)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrUnauthorized, nil)
		return
	}
	v1.HandleSuccess(ctx, token)
}

// GetDriverLoginInfo 获取登录之后的司机信息
func (h *DriverHandler) GetDriverLoginInfo(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	driverInfoVo, err := h.driverService.GetDriverInfo(ctx, driverID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, driverInfoVo)

}

// GetDriverAuthInfo 获取司机的认证信息
func (h *DriverHandler) GetDriverAuthInfo(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	driverAuthInfoVo, err := h.driverService.GetDriverAuthInfo(ctx, driverID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, driverAuthInfoVo)
}

// UpdateDriverAuthInfo 更新司机认证信息
func (h *DriverHandler) UpdateDriverAuthInfo(ctx *gin.Context) {
	req := new(v1.UpdateDriverAuthInfoForm)
	if err := ctx.ShouldBindJSON(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	driverID := ctx.GetInt64("ID")
	req.DriverID = int(driverID)
	isSuccess, err := h.driverService.UpdateDriverAuthInfo(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, isSuccess)
}

// CreateDriverFaceModel 创建司机人脸模型
func (h *DriverHandler) CreateDriverFaceModel(ctx *gin.Context) {
	req := new(v1.DriverFaceModelForm)
	if err := ctx.ShouldBindJSON(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	driverID := ctx.GetInt64("ID")
	req.DriverID = int(driverID)
	isSuccess, err := h.driverService.CreateDriverFaceModel(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, isSuccess)
}

// IsFaceRecognition 判断司机是否已人脸识别
func (h *DriverHandler) IsFaceRecognition(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	isSuccess, err := h.driverService.IsFaceRecognition(ctx, driverID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, isSuccess)
}

// VerifyDriverFace 验证司机人脸
func (h *DriverHandler) VerifyDriverFace(ctx *gin.Context) {
	var driverFaceModelForm v1.DriverFaceModelForm
	if err := ctx.ShouldBindJSON(&driverFaceModelForm); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	driverFaceModelForm.DriverID = int(ctx.GetInt64("ID"))
	flag, err := h.driverService.VerifyDriverFace(ctx, driverFaceModelForm)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// StartService 开启接单服务
func (h *DriverHandler) StartService(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	flag, err := h.driverService.StartService(ctx, driverID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	if !flag {
		v1.HandleError(ctx, v1.AuthErr, v1.ErrNotAuth, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// StopService 停止接单服务
func (h *DriverHandler) StopService(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	flag, err := h.driverService.StopService(ctx, driverID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}



